package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//默认当前类全部返回json格式
@RestController//扫描当前类，spring创建当前类的对象，默认调用无参构造
@RequestMapping("/employee")
@Api(value = "员工管理",description = "员工的CRUD")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    //查询所有Employee  Restful风格
    // http://localhost:8080/employee  -- GET
    @GetMapping
    @ApiOperation(value = "查询所有员工")
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    //根据id 查询Employee  Restful
    // http://localhost:8080/employee/1  -- GET
    //@PathVariable 取地址栏的参数
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询员工")
    public Employee findOne(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }

    //分页查询
    //http://localhost:8080/employee -- Post
    //多个参数根据post提交请求，参数是json格式{},请求方式就是post
    //@RequestBody 前端请求为json格式，接收后封装给参数 EmployeeQuery employeeQuery
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    //删除员工
    //http://localhost:8080/employee/1  -- DELETE
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            employeeService.delById(id);
            return new AjaxResult();//成功
        } catch (Exception e) {
            e.printStackTrace();
            //旧方式
            //return new AjaxResult(false,e.getMessage());
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }
    }

    //新增和修改 共用一个方法
    // http://localhost:8080/employee -- put
    //@RequestBody 前端请求为json格式，接收后封装给参数
    @PutMapping
    @ApiOperation(value = "新增或者修改员工信息")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId()==null) { //id为null是新增
                employeeService.add(employee);//新增
            } else {//修改，id不为null就是修改
                employeeService.update(employee);
            }
            return new AjaxResult();//成功
        } catch (Exception e) {
            e.printStackTrace();
            //旧方式
            //return new AjaxResult(false,e.getMessage());
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }
    }

    //批量删除
    @PatchMapping
    @ApiOperation("批量删除")
    public AjaxResult parchDel(@RequestBody List<Long> ids){
        try {
            employeeService.parchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }
    }
}
